﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using App.Utilities.Web.Handlers;
using CRMBiz;
using Newtonsoft.Json;
using System.Data;
using CRMEntity;

namespace CRMWeb.WebServices
{
    /// <summary>
    /// Summary description for History
    /// </summary>
    public class History : BaseHandler
    {

        public override void OnMethodInvoke(OnMethodInvokeArgs e)
        {
            context.Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
            context.Response.Cache.SetExpires(DateTime.MinValue);
            context.Response.Cache.SetNoStore();

            //e.Cancel = true;

            SetResponseContentType(ResponseContentTypes.HTML);
        }


        public override void AfterMethodInvoke(object result)
        {
            base.AfterMethodInvoke(result);
        }

        // Get Single History Record by HistoryID 
        public void SelectHistoryByID(string historyType, int historyID)
        {
            try
            {
                DataSet ds = HistoryHandler.SelectHistoryByID(historyType, historyID);
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void saveNewHistory(string HistoryType, int contactid, string hdate, string htime, string action, string user, string description, int appid)
        {
            HistoryInfo historydata = new HistoryInfo();

            historydata.historyDate = hdate;
            historydata.historyTime = htime;
            historydata.action = action;
            historydata.oprator = user;
            historydata.description = description;

            if (HistoryType == "Contact")
            {
                historydata.contactID = contactid;
                historydata.appID = 0;
                HistoryHandler.AddHistory(historydata);
            }
            else if (HistoryType == "Application")
            {
                historydata.contactID = 0;
                historydata.appID = appid;
                HistoryHandler.AddHistory(historydata);
            }

        }

        public void SelectAllHistoryByType(string historyType, int ID)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);
                DataSet ds = HistoryHandler.SelectAllHistoryByType(historyType, ID, pageIndex, pageSize);
                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


        public void ApplicationHistorySelectAllByAction(int app_id, string Action)
        {
            try
            {
                DataTable dt = HistoryHandler.GetAllApplicationHistoryByAction(app_id, Action);
                string strJson = JsonConvert.SerializeObject(dt);
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }

        public void SelectAllHistoryNotFieldChange(string historyType, int ID)
        {
            try
            {
                var pageIndex = string.IsNullOrEmpty(context.Request["page"]) ? 1 : int.Parse(context.Request["page"]);
                var pageSize = string.IsNullOrEmpty(context.Request["rows"]) ? 10 : int.Parse(context.Request["rows"]);

                DataSet ds = HistoryHandler.GetHistoryNotFieldChange(historyType, pageIndex, pageSize, ID);

                var total = (ds.Tables[0].Rows.Count > 0) ? Convert.ToInt32(ds.Tables[0].Rows[0]["Total"]) : 0;
                string strJson = JsonConvert.SerializeObject(ds.Tables[0]);
                strJson = "{\"total\":\"" + total.ToString() + "\",\"rows\":" + strJson + "}";
                context.Response.Write(strJson);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
}